package com.ecloud.trend.service.impl;

import TrendDB.*;
import com.ecloud.trend.dto.TrendSectionDto;
import com.ecloud.trend.vo.TrendDataVo;
import com.ecloud.trend.vo.TrendHistPositionVo;
import com.ecloud.trend.vo.TrendHistStatusVo;
import com.ecloud.trend.service.TrendService;
import com.ecloud.trend.utils.TrendTemplate;
import com.ecloud.trend.utils.TrendUtil;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Trend服务实现类
 *
 * @author Squbi
 * @date 2021/6/28 16:32
 */
@Service
@RequiredArgsConstructor
public class TrendServiceImpl implements TrendService {
    private final Logger logger = LoggerFactory.getLogger(TrendServiceImpl.class);

    private final TrendTemplate template;

    @Override
    public boolean testConn() {
        return template.testConn();
    }

    @Override
    public TrendHistPositionVo histPosition(String tag) {
        HistoryPosition historyPosition = template.queryHistPosition(tag);
        return TrendUtil.transfer(historyPosition);
    }

    @Override
    public TrendHistStatusVo histStatus(String tag) {
        HistoryStatus historyStatus = template.queryHistStatus(tag);
        return TrendUtil.transfer(historyStatus);
    }

    @Override
    public List<TrendDataVo> trendAll(String tag) {
        DB_Value[] dbValues = template.queryTrendAll(tag);
        return TrendUtil.transfer(dbValues);
    }

    @Override
    public List<TrendDataVo> lastValue(String tag) {
        DB_Value[] dbValues = template.queryLatestValue(tag);
        return TrendUtil.transfer(dbValues);
    }

    @Override
    public List<TrendDataVo> lastValues(String[] tags) {
        DB_Value[] dbValues = template.queryLatestValue(tags);
        return TrendUtil.transfer(dbValues);
    }

    @Override
    public List<TrendDataVo> sectionData(TrendSectionDto sectionDto) {
        HistoryPosition historyPosition = new HistoryPosition();
        historyPosition.start = TrendUtil.transfer(sectionDto.getStart());
        historyPosition.current = API.TimeNow();
        historyPosition.end = TrendUtil.transfer(sectionDto.getEnd());
        logger.debug("historyPosition out > {}", historyPosition);
        HistoryPosition position = template.queryHistPosition(sectionDto.getTag(), historyPosition);
        logger.debug("historyPosition in > {}", position);
        DB_Value[] dbValues = template.queryTrend(sectionDto.getTag(), position);
        return TrendUtil.transfer(dbValues);
    }
}
